package com.example.demo.entity;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author : 叶荻风
 * @create : 2024/5/8 16:40
 */
@Component("annotationBean")
public class AnnotationBean implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    private String name;

    public AnnotationBean(){
    }

    @PostConstruct
    public void init(){
        name = applicationContext.getBeanNamesForType(AnnotationBean.class)[0];
        System.out.println("Bean "+name+" init");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("Bean "+name+" destroy");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
